package backend_agent

import (
	"context"
	"xialou_platform/app/errorx"
	"xialou_platform/app/respx"
	"xialou_platform/services/agent/rpc/agentClient"

	"xialou_platform/services/agent/api/internal/svc"
	"xialou_platform/services/agent/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AgentChangeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAgentChangeLogic(ctx context.Context, svcCtx *svc.ServiceContext) AgentChangeLogic {
	return AgentChangeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AgentChangeLogic) AgentChange(req *types.AgentChangeReq) error {
	agentParam, err := l.svcCtx.AgentRpc.AgentById(l.ctx, &agentClient.IdBody{Id: respx.IDParse(req.Id).GetInt64()})
	if err != nil {
		return err
	}

	if agentParam.Status == 0 {
		return errorx.NewDefaultError("请通知该销氪先做实名认证才能开通。")
	}

	agentParam.Status = 2
	_, err = l.svcCtx.AgentRpc.AgentUpdate(l.ctx, agentParam)
	if err != nil {
		return err
	}

	return nil
}
